﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace socialnexus
{
    public partial class EntryPoint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            socialnexus.classes.models.EntryPoint ep;

            if (Request.QueryString["GUID"] != null)
            {
                ep = socialnexus.classes.models.EntryPoint.GetByGUID(Request.QueryString["GUID"].ToString());
                if (ep == null)
                {
                    Response.Redirect("~/");
                }
                else
                {

                    HttpCookie cookie = new HttpCookie("EntryPoint");
                    cookie.Values.Add("GUID", ep.GUID);
                    cookie.Expires = DateTime.MaxValue;
                    cookie.Path = "/";
                    cookie.HttpOnly = true;
                    Response.Cookies.Add(cookie);
                    Request.Cookies.Add(cookie);
                }
            }

            if (Request.Cookies.Get("EntryPoint") == null)
            {
                Response.Redirect("~/");
            }
            else
            {
                ep = socialnexus.classes.models.EntryPoint.GetByGUID(Request.Cookies.Get("EntryPoint").Values["GUID"]);
                if (ep == null)
                {
                    Response.Cookies.Get("EntryPoint").Expires = DateTime.Now.AddDays(-1);
                    Response.Redirect("~/EntryPoint.aspx");
                    return;
                }
                DataTable users = ep.getUsers();
                rptrEntryPoint.DataSource = users;
                rptrEntryPoint.DataBind();
            }
        }

        protected void rptrEntryPoint_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;

            HyperLink hlMessage = (HyperLink)e.Item.FindControl("hlMessage");
            System.Web.UI.WebControls.Image imgFriend = (System.Web.UI.WebControls.Image)e.Item.FindControl("imgFriend");

            hlMessage.NavigateUrl = "~/EntryPointAuth.aspx?UserId=" + row["SiteUserId"].ToString();
            hlMessage.CssClass = "noMessage";

            imgFriend.ImageUrl = row["PictureURL"].ToString().Trim();
        }
    }
}
